from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates

app = FastAPI()

# 设置模板目录
templates = Jinja2Templates(directory="templates")

# 虚拟数据库
users = {
    "username": "password",
    "1": "1"
}

# 首页，返回登录页面
@app.get("/", response_class=HTMLResponse)
async def login_page(request: Request):
    return templates.TemplateResponse("login.html", {"request": request})

# 处理登录请求
@app.post("/login")
async def login(username: str = Form(...), password: str = Form(...)):
    if username in users and users[username] == password:
        return JSONResponse({"status": "success", "message": "登录成功！"})
    else:
        return JSONResponse({"status": "error", "message": "用户名或密码错误！"})